package com.atguigu.gmall.common.utils;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.user.vo.UserAuthInfoVo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author:ahang
 * @create:2022-10-20 15:30
 * @Description:
 */
public class UserAuthUtils {

    //返回用户id和临时id工具类
    public static UserAuthInfoVo getUserAuthInfo(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String userId = request.getHeader(RedisConst.User_ID_HEADER);
        String tempId = request.getHeader(RedisConst.User_TEMP_ID_HEADER);

        Long uid = null;
        UserAuthInfoVo infoVo = new UserAuthInfoVo();
        if(userId != null){ //防止userid为空出空指针异常
            uid = Long.parseLong(userId);
        }
        infoVo.setUserId(uid);
        infoVo.setTempId(tempId);

        return infoVo;
    }
}
